package com.netrust.cmsx.admin.modules.sys.web;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.netrust.cmsx.admin.modules.sys.pojo.MenuPo;
import com.netrust.cmsx.admin.modules.sys.service.MenuService;
import com.netrust.cmsx.admin.modules.sys.utils.UserUtils;
import com.netrust.cmsx.core.config.Global;
import com.netrust.cmsx.core.page.PageKey;
import com.netrust.cmsx.core.page.jq.JqGrid;
import com.netrust.cmsx.core.page.jq.JqPost;
import com.netrust.cmsx.core.page.jq.convert.Page2JQ;
import com.netrust.cmsx.core.resp.RespJson;
import com.netrust.cmsx.core.web.BaseController;
import com.netrust.cmsx.entity.module.sys.Menu;
import com.netrust.cmsx.entity.module.sys.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * 栏目Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/permission")
public class PermissionController extends BaseController {

	@Autowired
	private MenuService menuService;
	
	@ResponseBody
	@RequestMapping(value = {""})
	public Menu get(@RequestParam(required=false) String id) {
		if (id!=null){
			return menuService.findOne(id);
		}else{
			return new Menu();
		}
	}
	
	@RequestMapping(value = {"form"})
	public String form(@RequestParam(required=false) String id,@RequestParam(required=false) String pid,Model model) {
		Menu menu=new Menu();
		if(id!=null){//代表修改跳转
			menu=menuService.findOne(id);	
		}
		
		model.addAttribute("menu", menu);
		return "modules/sys/permissionForm";
	}
	
	@RequestMapping(value = {"save"})
	public String save(Menu menu) {		
		menu.setType("permission");
		menuService.saveMenu(menu);
		
		return "modules/sys/permissionList";
	}
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = {"del"})
	public RespJson del(@RequestParam(required=true) String id) {
		try {
			menuService.deleteInFlag(new Menu(id));
			return new RespJson(Global.Operate_Success, 200);
		} catch (Exception e) {
			return new RespJson(Global.Operate_Error, 500, null);
		}
	}

	@ResponseBody
	@RequestMapping(value = {"list"})
	public JqGrid list(JqPost jqPost,String key) {
		PageKey pk=new PageKey(key, jqPost.getPage(),jqPost.getRows());
		Page<Menu> page =menuService.findPermissionList(pk);
		JqGrid jg=new Page2JQ<Menu>().run(page,new String[]{"id","name","permission","sort","remarks"});		
		return jg;
	}

	
	
}
